import { h } from "vue"
import { NButton } from "naive-ui"

interface ColMeta {
  title: string
  key: string
  render?: Function
}

interface OptAction {
  name: string
  action: Function
}

const renderTableCols = (colMetas: Array<ColMeta>, actions?: Array<OptAction>) => {
  let tabCols: Array<any> = []
  colMetas.forEach((col) => {
    if (col.key === "#" && actions) {
      col.render = (row: any) => {
        const opts = actions.map((action) => {
          return h(
            NButton,
            {
              size: "small",
              style: {
                marginRight: "6px",
              },
              onClick: () => action.action(row),
            },
            {
              default: () => action.name,
            }
          )
        })
        return opts
      }
    }
    tabCols.push(col)
  })
  return tabCols
}

export { renderTableCols }
